package com.yuyu.service.impl;

/***********************************************************************
 * Module:  EmployeeServcieImpl.java
 * Author:  xiaoyuyu
 * Purpose: Defines the Class EmployeeServcieImpl
 ***********************************************************************/

import com.yuyu.bean.Employee;
import com.yuyu.bean.EmployeePageDataOfMybatis;
import com.yuyu.exception.*;
import com.yuyu.exception.EmployeeException.AddEmployeeException;
import com.yuyu.exception.EmployeeException.DeleteEmployeeException;
import com.yuyu.exception.EmployeeException.EditEmployeeException;
import com.yuyu.exception.EmployeeException.EmployeeNotExsitException;
import com.yuyu.mapper.EmployeeMapper;
import com.yuyu.uitlbean.*;
import com.yuyu.service.EmployeeService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/** @pdOid 43c876ac-9b57-43aa-8fe3-e715f0bbe32d */
@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public ResultObj addEmployee(Employee employee) {
        int result = employeeMapper.addEmployee(employee);
        ResultObj resultObj = new ResultObj(MessageCode.SUCCESS);
        if(result == 0){
            throw new AddEmployeeException("添加失败");
        }
        return resultObj;
    }

    @Override
    public ResultObj deleteEmployeeById(List<Integer> list) {
        int result = employeeMapper.deleteEmployeeById(list);
        ResultObj resultObj = new ResultObj(MessageCode.SUCCESS);
        if(result == 0){
            throw new DeleteEmployeeException("删除失败");
        }
        return resultObj;
    }

    @Override
    public ResultObj editEmployee(Employee employee) {
        int result = employeeMapper.editEmployee(employee);
        ResultObj resultObj = new ResultObj(MessageCode.SUCCESS);
        if(result == 0){
            throw new EditEmployeeException("编辑失败");
        }
        return resultObj;


    }

    @Override
    public ResultObj<ResultPageData<List<Employee>>> filtrateEmployee(PageData pageData,String condition) {

        EmployeePageDataOfMybatis employeePageDataOfMybatis = EmployeePageDataOfMybatis.handle(pageData);
        employeePageDataOfMybatis.setCondition(condition);
        //获得指定页的数据
        List<Employee> employees = employeeMapper.filtrateEmployee(employeePageDataOfMybatis);
        //定义返回数据对象
        ResultObj<ResultPageData<List<Employee>>> resultObj = new ResultObj<>();
        //定义页面数据对象
        ResultPageData<List<Employee>> resultPageData = new ResultPageData<>();
        //获得数据总量
        int employeeCount = employeeMapper.getEmployeeVarNumForCondition(condition);
        //赋值
        resultPageData.setCount(employeeCount);
        resultPageData.setData(employees);
        resultObj.setData(resultPageData);
        return resultObj;
    }

    @Override
    public ResultObj<Employee> getEmployeeByUserName(String username) {
        Employee employee = employeeMapper.getEmployeeByUserName(username);
        //员工资料不存在
        if (employee == null){
            throw new EmployeeNotExsitException("不存在");
        }
        ResultObj<Employee> resultObj = new ResultObj<>();
        resultObj.setData(employee);
        return resultObj;
    }

    @Override
    public ResultObj<Employee> getEmployeeById(Integer id) {
        ResultObj<Employee> resultObj = new ResultObj<>();
        Employee employee = employeeMapper.getEmployeeById(id);
        if(employee == null){
            throw new EmployeeNotExsitException("不存在");
        }
        return resultObj;
    }

    @Override
    public XSSFWorkbook exportEmployeeExcel() {
        List<Map<String,Object>> employees = employeeMapper.getAllEmployeeExportExcel();
        List<ExcelBean> excel = new ArrayList<>();
        Map<Integer,List<ExcelBean>> map = new LinkedHashMap<>();
        //设置标题栏
        excel.add(new ExcelBean("编码","id",0));
        excel.add(new ExcelBean("名称","name",0));
        excel.add(new ExcelBean("性别", "sex", 0));
        excel.add(new ExcelBean("生日","birthday",0));
        excel.add(new ExcelBean("电话","telephone",0));
        excel.add(new ExcelBean("住址","address", 0));
        excel.add(new ExcelBean("学历","education",0));
        excel.add(new ExcelBean("身份证号","id_number",0));
        excel.add(new ExcelBean("上岗时间","duty_time",0));
        excel.add(new ExcelBean("职务","duty",0));
        excel.add(new ExcelBean("用户名","username",0));
        excel.add(new ExcelBean("密码","password",0));
        excel.add(new ExcelBean("启用","start_using",0));
        map.put(0,excel);
        String sheetName = "用户信息表";
        //调用ExcelUtil方法
        XSSFWorkbook xssfWorkbook = null;
        try {
            xssfWorkbook = ExcelUtil.createExcelFile(Employee.class, employees, map, sheetName);
        } catch (IllegalAccessException e) {
            throw new ExportExcelException("导出失败");
        } catch (InvocationTargetException e) {
            throw new ExportExcelException("导出失败");
        } catch (IntrospectionException e) {
            throw new ExportExcelException("导出失败");
        }
        System.out.println(xssfWorkbook);
        return xssfWorkbook;
    }

    @Override
    public ResultObj importEmployeeExcel(XSSFSheet sheet) {
        ResultObj resultObj = new ResultObj();
        StringBuilder msg = new StringBuilder();
        for (int i = 1;i <= sheet.getLastRowNum(); i ++){
            //默认不上传id，自动生成
//            XSSFCell id = sheet.getRow(i).getCell(0);
            XSSFCell name = sheet.getRow(i).getCell(1);
            XSSFCell sex = sheet.getRow(i).getCell(2);
            XSSFCell birthday = sheet.getRow(i).getCell(3);
            XSSFCell telephone = sheet.getRow(i).getCell(4);
            XSSFCell address = sheet.getRow(i).getCell(5);
            XSSFCell education = sheet.getRow(i).getCell(6);
            XSSFCell id_number = sheet.getRow(i).getCell(7);
            XSSFCell duty_time = sheet.getRow(i).getCell(8);
            XSSFCell duty = sheet.getRow(i).getCell(9);
            XSSFCell username = sheet.getRow(i).getCell(10);
            XSSFCell password = sheet.getRow(i).getCell(11);
            XSSFCell start_using = sheet.getRow(i).getCell(12);

  /*          if(id == null || id.toString().equals("")){
                throw new ImportExcelException("错误行:"+ (i+1) +"编码不能为空");
            }*/

            if (username == null || username.toString().equals("")){
                throw new ImportExcelException("错误行: \""+ (i+1) +"\" 登录名不能为空");

            }

            Employee _employee = employeeMapper.getEmployeeByUserName(username.toString());
            if (_employee != null){
//                throw new ImportExcelException(username.toString() + "已经存在，跳过此行");
                msg.append("[登录名：\"" + username.toString() + "\" 已经存在，跳过此行]|");
                continue;
            }

            if(!(start_using.toString().equals("0") || start_using.toString().equals("1"))){
                throw new ImportExcelException("启用状态值应该为\"0\"或者\"1\"");
            }

            Employee employee = new Employee();
            employee.setName(name.toString());
            employee.setSex(sex.toString());
            employee.setBirthday(birthday.toString());
            employee.setTelephone(telephone.toString());
            employee.setAddress(address.toString());
            employee.setEducation(education.toString());
            employee.setId_number(id_number.toString());
            employee.setDuty_time(duty_time.toString());
            employee.setDuty(duty.toString());
            employee.setUsername(username.toString());
            employee.setPassword(password.toString());
            employee.setStart_using(Integer.parseInt(start_using.toString()));

            System.out.println(employee);

            int result = employeeMapper.addEmployee(employee);
            if (result == 0){
                msg.append("[登录名：" + employee.getUsername() + "添加失败]|");
            }

        }
        if(!msg.toString().equals("")){
            resultObj.setMsg(msg.toString());
        }

        return resultObj;
    }

}
